package initialization;

import ga.GAInfo;
import ga.Individual;

import instrumentation.BlockClass;
import instrumentation.TrackerCoverage;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import ReadWGZ.ReadFilewgzMDR;


import mdr.MethodDependences;
import mdr.RecommendGenerator;
import model.CUT;
import model.Filter;
import model.TestClusterBuilder;
import util.GAConfig;
import util.GAOperationsConfig;
import util.Log;

public class TestInitalizationPopulation {
		public static void main(String[] args) throws IOException,
		ClassNotFoundException{
			
			String logFilePath = "/scratch/workspace/GA/GA-1/logFiles/population.txt";
			Log.logConfig(logFilePath);
			new File(logFilePath).getAbsolutePath();
			
			
			long startTime = Calendar.getInstance().getTimeInMillis();
	    	  PopulationInitializer populationInitializer = GAOperationsConfig.getPopulationInitializer();

			ReadFilewgzMDR readFileWGZ = new ReadFilewgzMDR();
			 TrackerCoverage tracker=  TrackerCoverage.getTracker();
			GAInfo gaInfo=  new GAInfo();
			gaInfo.setTracker(tracker);
			gaInfo.setTestTimeOut( 60 * 10000); ;
			gaInfo.setStartTime(startTime);
				 Set<Class<?>> allClasses = new LinkedHashSet<Class<?>>();
			      //get class from file
			      allClasses.addAll(TestClusterBuilder.readClassFromFile());

			      System.out.println("There are " + allClasses.size() + " classes as input");
			      
			      
			      System.out.println(allClasses);

			      List<TestClusterBuilder> TestClusterList= TestClusterBuilder.buildTestClusterList(allClasses);
					 Map<String, BlockClass> blockClasses=readFileWGZ.readFileWGZ(TestClusterList);
					 Map<String,Set<Integer>> classBranches=readFileWGZ.getClazzCondition(blockClasses);

					gaInfo.setMapBlockClasses(blockClasses);
					gaInfo.setClazzBranches(classBranches);

			      for(TestClusterBuilder tCluster:TestClusterList){
						//the method recommender
						 if(GAConfig.UseReadWrietRelation||!GAConfig.randomInitialPopulation){
							 System.out.println( "Building up method dependence model for diversifying...");
							 Set<Class<?>> cluster = new LinkedHashSet<Class<?>>();
								for(String cName:tCluster.getCluster().keySet()){
									Class<?> cls=tCluster.getCluster().get(cName).getClazz();
									if(Filter.isPrimitiveOrStringType(cls))continue;
									if(cls.getName().startsWith("java.")) continue;
									if(cls.getName().startsWith("sun.")) continue;
									if(cls.getName().contains("$")) continue;
									if(!Filter.canUse(cls))continue;
									cluster.add(cls);
								}
								gaInfo.setTestCluster(tCluster);
								MethodDependences recommender = new MethodDependences(cluster);
								 recommender.buildDependence(tCluster.getStatements());
								 System.out.println("Finishing method dependence model building");
								 gaInfo.setMethodRelations(recommender);
								 
								 ////print out the relation methods dependency
									Set<Class<?>> classToTest= new LinkedHashSet<Class<?>>();

									for(Class<?> cls: tCluster.getClassToTest().values())
										classToTest.add(cls);
								System.out.println("--------------------");
								System.out.println("     CUT: "+ classToTest);
								System.out.println("--------------------");
								System.out.println(recommender.showDependence(classToTest));
								System.out.println(recommender.showStatmentsWriteFileds(classToTest));
								RecommendGenerator recommendGenerator=new  RecommendGenerator(gaInfo);
								 gaInfo.setRecommendGenerator(recommendGenerator);
								 System.out.println(recommendGenerator.toString());
						 }
			     
						CUT cut = tCluster.getCut();
						
						Map<String, Class<?>> classesToTest = tCluster.getClassToTest();
						
						String clz = classesToTest.keySet().iterator().next();
						System.out.println("Class " + clz + " is being Tested");

					  gaInfo.setClzToTest(cut.getClazz());
			    	  gaInfo.setTestCluster(tCluster);

			    	  populationInitializer.setExtraInfo(gaInfo);
			    	 GAConfig.randomInitialPopulation=false;
			    		Individual[] population = populationInitializer.createPopulation();			 
			    		gaInfo.setPopulation(population);
			    		populationInitializer.printPopulation();

			      }
					System.exit(0);
		}

}
